package com.ebupt.migu.music.redis.controller;

import com.ebupt.migu.music.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Set;

/**
 * @author admin
 */
@RestController
@RequestMapping("/redis")
public class RedisController {


    @Autowired
    private RedisService redisService;


    @RequestMapping(value = "/set")
    public void set(String key, String value) {
        redisService.set(key, value);
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public String get(String key) {
        return redisService.get(key).toString();
    }

    @RequestMapping(value = "/getSet")
    @ResponseBody
    public String getSet(String key) {
        return redisService.getSet(key).toString();
    }

    @RequestMapping(value = "/test")
    public void test() {
        // 交集测试
        Set<String> set1 = new HashSet<>();
        set1.add("1");
        set1.add("2");
        set1.add("3");
        set1.add("4");
        set1.add("5");


        Set<String> set2 = new HashSet<>();
        set2.add("3");
        set2.add("4");
        set2.add("5");
        set2.add("6");
        set2.add("7");


        // 交集
        redisService.intersectAndStore("test_set1", "test_set2", "set_intersect");
        // 并集
        redisService.unionAndStore("test_set1", "test_set2", "set_union");
        // 差集
        redisService.differenceAndStore("test_set1", "test_set2", "set_difference");
    }

    @RequestMapping(value = "/type")
    public String type(String key) {
        return redisService.type(key).toString();
    }

}